
<!DOCTYPE html
  PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <!--
This HTML is auto-generated from an M-file.
To make changes, update the M-file and republish this document.
      --><title>chol3SDX</title><meta name="generator" content="MATLAB 7.9"><meta name="date" content="2011-02-03"><meta name="m-file" content="chol3SDX"><style type="text/css">

body {
  background-color: white;
  margin:10px;
}

h1 {
  color: #990000; 
  font-size: x-large;
}

h2 {
  color: #990000;
  font-size: medium;
}

/* Make the text shrink to fit narrow windows, but not stretch too far in 
wide windows. */ 
p,h1,h2,div.content div {
  max-width: 600px;
  /* Hack for IE6 */
  width: auto !important; width: 600px;
}

pre.codeinput {
  background: #EEEEEE;
  padding: 10px;
}
@media print {
  pre.codeinput {word-wrap:break-word; width:100%;}
} 

span.keyword {color: #0000FF}
span.comment {color: #228B22}
span.string {color: #A020F0}
span.untermstring {color: #B20000}
span.syscmd {color: #B28C00}

pre.codeoutput {
  color: #666666;
  padding: 10px;
}

pre.error {
  color: red;
}

p.footer {
  text-align: right;
  font-size: xx-small;
  font-weight: lighter;
  font-style: italic;
  color: gray;
}

  </style></head><body><div class="content"><h2>Contents</h2><div><ul><li><a href="#3"><b>Inputs</b></a></li><li><a href="#4"><b>Outputs</b></a></li><li><a href="#5">Check dimensions and parameters in bounds</a></li><li><a href="#6">Unpack</a></li><li><a href="#7">Solve coefficients SDX=[a b c;0 d e;0 0 f]</a></li><li><a href="#8">If flag_check==1, check matrix is PD</a></li></ul></div><pre class="codeinput"><span class="keyword">function</span> [SDX,flag_ok,V]=chol3SDX(parv,flag_check)
</pre><pre class="codeinput"><span class="comment">%=========================================================================</span>
<span class="comment">% [SDX,flag_ok,V]=chol3SDX(parv,flag_check)</span>
<span class="comment">%</span>
<span class="comment">% Given the 6 elements of the [3 3] Covariance Matrix, V, construct SDX=chol(V) such that</span>
<span class="comment">% V=SDX'*SDX while allowing for correlated elements.</span>
<span class="comment">%</span>
</pre><h2><b>Inputs</b><a name="3"></a></h2><p>PARV [6 1] with entried 1) sigma_11 2) sigma_22 3) sigma_33 4) rho_12 5) rho_13 6) rho_23</p><p>FLAG_CHECK   Optional, if ==1 check resulting V matrix is PD              default is zero, in which case flag_ok==1 does not mean              matrix has been checked</p><h2><b>Outputs</b><a name="4"></a></h2><p>SDX           V=SDX'SDX flag_ok       if flag_check=1, means V is P.D. V</p><p>Alejandro Justiniano Feb 3rd 2011</p><pre class="codeinput"><span class="comment">%=========================================================================</span>
<span class="keyword">if</span> nargin &lt; 2 || flag_check~=1;
    flag_check=0;
<span class="keyword">end</span>
</pre><h2>Check dimensions and parameters in bounds<a name="5"></a></h2><pre class="codeinput">flag_ok=0; SDX=[];
<span class="keyword">if</span> length(parv)~=6;error(<span class="string">'PARV must be 6x1'</span>);<span class="keyword">end</span>
<span class="keyword">if</span> any( parv(1:3) ) &lt; eps; error(<span class="string">'First 3 entries of PARV must be standard deviations'</span>);<span class="keyword">end</span>
<span class="keyword">if</span> any( abs(parv(4:6)) &gt; 1 ); error(<span class="string">'Last 3 entries of PARV must be correlations'</span>);<span class="keyword">end</span>
</pre><pre class="codeoutput">Input argument "parv" is undefined.

Error in ==&gt; chol3SDX at 33
if length(parv)~=6;error('PARV must be 6x1');end 
</pre><h2>Unpack<a name="6"></a></h2><pre class="codeinput">sigma_11 = parv(1);
sigma_22 = parv(2);
sigma_33 = parv(3);
rho_12   = parv(4);
rho_13   = parv(5);
rho_23   = parv(6);
</pre><h2>Solve coefficients SDX=[a b c;0 d e;0 0 f]<a name="7"></a></h2><pre class="codeinput">a=sigma_11;
b=rho_12*sigma_22;
c=rho_13*sigma_33;
d=sigma_22*sqrt( 1-rho_12*rho_12 );
e=( rho_23*sigma_22*sigma_33 - b*c )/d;
ftemp=sigma_33*sigma_33 - e*e - c*c;
<span class="keyword">if</span> ftemp &lt; eps; <span class="keyword">return</span>; <span class="keyword">end</span>
f=sqrt(ftemp);

SDX=zeros(3,3);
SDX(1,1)=a;
SDX(1,2)=b;
SDX(1,3)=c;
SDX(2,2)=d;
SDX(2,3)=e;
SDX(3,3)=f;

<span class="keyword">if</span> flag_check==0;
    flag_ok=1; V=[];
    <span class="keyword">return</span>
<span class="keyword">end</span>
</pre><h2>If flag_check==1, check matrix is PD<a name="8"></a></h2><pre class="codeinput">V=(SDX'*SDX);
V=0.5*(V+V');
[~,flag_nopd]=chol(V);
<span class="keyword">if</span> flag_nopd==0;flag_ok=1;<span class="keyword">end</span>;
</pre><p class="footer"><br>
      Published with MATLAB&reg; 7.9<br></p></div><!--
##### SOURCE BEGIN #####
function [SDX,flag_ok,V]=chol3SDX(parv,flag_check)
%=========================================================================
% [SDX,flag_ok,V]=chol3SDX(parv,flag_check)
%
% Given the 6 elements of the [3 3] Covariance Matrix, V, construct SDX=chol(V) such that
% V=SDX'*SDX while allowing for correlated elements. 
% 
%% *Inputs*
% PARV [6 1] with entried  
% 1) sigma_11 
% 2) sigma_22
% 3) sigma_33
% 4) rho_12
% 5) rho_13
% 6) rho_23 
% 
% FLAG_CHECK   Optional, if ==1 check resulting V matrix is PD
%              default is zero, in which case flag_ok==1 does not mean
%              matrix has been checked 
% 
%% *Outputs*
% SDX           V=SDX'SDX 
% flag_ok       if flag_check=1, means V is P.D. 
% V 
% 
% Alejandro Justiniano Feb 3rd 2011 
%=========================================================================
if nargin < 2 || flag_check~=1; 
    flag_check=0; 
end 
%% Check dimensions and parameters in bounds
flag_ok=0; SDX=[]; 
if length(parv)~=6;error('PARV must be 6x1');end 
if any( parv(1:3) ) < eps; error('First 3 entries of PARV must be standard deviations');end 
if any( abs(parv(4:6)) > 1 ); error('Last 3 entries of PARV must be correlations');end 

%% Unpack 
sigma_11 = parv(1); 
sigma_22 = parv(2); 
sigma_33 = parv(3); 
rho_12   = parv(4); 
rho_13   = parv(5); 
rho_23   = parv(6); 

%% Solve coefficients SDX=[a b c;0 d e;0 0 f]
a=sigma_11; 
b=rho_12*sigma_22; 
c=rho_13*sigma_33; 
d=sigma_22*sqrt( 1-rho_12*rho_12 ); 
e=( rho_23*sigma_22*sigma_33 - b*c )/d; 
ftemp=sigma_33*sigma_33 - e*e - c*c; 
if ftemp < eps; return; end 
f=sqrt(ftemp); 

SDX=zeros(3,3); 
SDX(1,1)=a; 
SDX(1,2)=b; 
SDX(1,3)=c; 
SDX(2,2)=d; 
SDX(2,3)=e; 
SDX(3,3)=f; 
 
if flag_check==0; 
    flag_ok=1; V=[]; 
    return 
end 

%% If flag_check==1, check matrix is PD 
V=(SDX'*SDX);
V=0.5*(V+V'); 
[~,flag_nopd]=chol(V); 
if flag_nopd==0;flag_ok=1;end; 
##### SOURCE END #####
--></body></html>